package com.itheima.reggie.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.db.Session;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Author fanYuLe
 * @Date 2023/3/2 12:12
 * @Blog blog.fanYuLe.net/996.mba
 * @Description:
 */
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private HttpSession session;
    //1.查询菜品分类
    @Override
    public List<Category> findAll() {
        List<Category> list=categoryMapper.findAll();
       return list;
    }
    //2.新增分类
    @Override
    public void save(Category category) {
        //1.补全信息
            //1-1雪花ID
        long categoryId = IdUtil.createSnowflake(1, 1).nextId();
        category.setId(categoryId);
//            //1-2创建、修改时间
//        category.setCreateTime(new Date());
//        category.setUpdateTime(new Date());
//            //1-3创建、修改人
//        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
//        category.setCreateUser(currentEmp.getId());
//        category.setUpdateUser(currentEmp.getId());
        //2.调用mapper新增
        categoryMapper.save(category);
    }

    //3.修改分类
    @Override
    public void update(Category category) {
        //1.补全信息
            //1-1修改时间
//        category.setUpdateTime(new Date());
//            //1-2修改人1mapper
//        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
//        category.setUpdateUser(currentEmp.getId());
        //2.调用mapper修改
        //categoryMapper.update(category);
        //将更新方法改成mybatisPlus提供的
        categoryMapper.updateById(category);
    }

    //4.删除分类
    @Override
    public void deleteById(Long id) {
        //1.先查询该分类是否有菜品
        Integer countDish=categoryMapper.countDishByCategoryId(id);
        if(countDish>0){
            //return ResultInfo.error("该分类下有菜品，无法删除");
            throw new CustomException("该分类下有菜品，无法删除");
        }
        //2.在查询该分类下是否有套餐
        Integer countSetmeal=categoryMapper.countSetmealByCategoryId(id);
        if(countSetmeal>0){
            //return ResultInfo.error("该分类下有套餐，无法删除");
            throw new CustomException("该分类下有套餐，无法删除");
        }
        //3.直接删除
        categoryMapper.deleteById(id);
       // return ResultInfo.success();
    }

    @Override
    public List<Category> list(Integer type) {
        //1.构建条件 select * from category where type=1
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType,type);
        return categoryMapper.selectList(qw);
    }
}

